﻿using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenIddict.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YDT_Distributed_Authentication.DistributedAuthentications
{
    /// <summary>
    /// 分布式权限客户端 封装
    /// </summary>
    public static class OpenIddictClientDistributedAuthenticationWebApplicationBuilderExtenions
    {
        public static WebApplicationBuilder AddDistributedAuthenticationClient(this WebApplicationBuilder builder)
        {
            var Configuration = builder.Configuration;

            // 6、集成Openiddict.Client
            builder.Services.AddOpenIddict()
                            // 配置客户端生成Token的信息
                            .AddClient(options => {
                                options.UseSystemNetHttp();
                                options.AllowPasswordFlow();

                                // 禁用令牌存储
                                options.DisableTokenStorage();

                                options.AddRegistration(new OpenIddictClientRegistration
                                {
                                    Issuer = new Uri(Configuration["DistributedAuthentication:OpenIddictClient:Issuer"]),
                                    ClientId = Configuration["DistributedAuthentication:OpenIddictClient:ClientId"],
                                    ClientSecret = Configuration["DistributedAuthentication:OpenIddictClient:ClientSecret"]
                                });

                                options.UseAspNetCore();
                            });
            return builder;
        }
    }
}
